package com.yunlinker.hotupdate.utils;

import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * description：单例线程池,用于处理一些异步任务
 */

public class ExecutorServiceHelper {
    private static ExecutorServiceHelper sExecutorServiceHelper;
    private ExecutorService mSingleThreadExecutor;

    protected Handler mHandler = new Handler(Looper.getMainLooper());

    public static ExecutorServiceHelper getInstance() {
        if (sExecutorServiceHelper == null) {
            synchronized (ExecutorServiceHelper.class) {
                if (sExecutorServiceHelper == null){
                    sExecutorServiceHelper = new ExecutorServiceHelper();
                }
            }
        }
        return sExecutorServiceHelper;
    }

    public synchronized void execute(Runnable runnable) {
        if (mSingleThreadExecutor == null) {
            mSingleThreadExecutor = Executors.newSingleThreadExecutor();
        }
        mSingleThreadExecutor.execute(runnable);
    }

    public void postToMainLooper(Runnable runnable) {
        mHandler.post(runnable);
    }

    public void postDelayToMainLooper(Runnable runnable, long ms) {
        mHandler.postDelayed(runnable, ms);
    }
}
